/**
 * 引入request封装
 * 实例化对象
 * 调用创建方法同时传入参数
 */
// import { commonUrl } from '../resource/deployUrl.js';
import {showToastMethod, reLaunchPage} from '../resource/uniMethods.js';
import Request from './request.js';
import { getStorageSyncData } from '../resource/until';
const httpClient = new Request()

httpClient.create({
  // 此为测试地址，需加入到域名白名单，或者更改为您自己的线上地址即可
  // baseUrl: commonUrl,
  dataType: 'json', // 可删除，默认为json
  responseType: 'text', // 可删除，默认为text
  // 设置请求头，支持所有请求头设置，也可不设置，去掉header就行
  header: {
    'token': 'token from global',
    'Content-Type': 'application/json',
  }
})

/**
 * 请求拦截器
 */
httpClient.interceptors.request(request => {
  // 如果请求体有值且值不为 FormData 格式请求处理为 {data: body} 格式
  let isTrue = false;
  
  // #ifdef MP-WEIXIN
	isTrue = request.data !== undefined
  // #endif
 // && !(request.data instanceof FormData || request.data instanceof URLSearchParams)  
  // #ifdef MP-BAIDU || H5
	isTrue = request.data !== undefined
  // #endif
	if ( isTrue ) {
  		if ( request.header["Content-Type"] === "application/x-www-form-urlencoded;charset=UTF-8" ) {
  		  request.data = request.data
  		}
  		if (request.header["Content-Type"] === "multipart/form-data") {
			const formData = new FormData();
			Object.keys(request.data).forEach(key => {
				if (request.data[key]) {
					if (Array.isArray(request.data[key])) {
						for (var i = 0; i < request.data[key].length; i++) {
							formData.append(key, request.data[key][i]);
						}
					} else {
						formData.append(key, request.data[key]);
					}
				}
			});
			request.data = formData;
  		}
  	} else {
  	}
    return request;
  });
/**
 * 响应拦截器
 */
httpClient.interceptors.response(response => {
  // 判断响应的 code 码是否正常
  const responseData = response.data;
  uni.hideLoading();
  if (responseData && responseData.code) {
	  // [200, '200'].indexOf(responseData.code) !== -1 && 
    if (responseData.code == '200') {
      return Promise.resolve(responseData.data);
    } else {
	// reLaunchPage('/subPackages/views/login/accountLogin')
      showToastMethod(responseData.message, '3')
      return false
    }
  } else {
    return Promise.resolve(responseData.message)
  }
  const err = new Error(responseData.message || response.statusText);
  err.errorCode = responseData.code;
  return Promise.reject(err);
});
/**
 *创建对象，定义方法， 同时传入参数，并导出
 * @param url
 * @param data
 * @param header
 * @param config
 * @param dataType
 * @returns {Promise<*>}
 * @constructor
 */
const APIClientService = {
  async REQUESTPOST(url, data, header, config, dataType) {
    return httpClient.REQUESTMAPPING('POST', url, data || {}, header, config, dataType)
  },
  async REQUESTGET(url, data, header, config, dataType) {
    return httpClient.REQUESTMAPPING('GET', url, data || {}, header || {
      "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
    }, config, dataType)
  },
  async REQUESTPUT(url, data, header, config, dataType) {
    return httpClient.REQUESTMAPPING('PUT', url, data || {}, header, config, dataType)
  },
  async REQUESTDELETE(url, data, header, config, dataType) {
    return httpClient.REQUESTMAPPING('DELETE', url, data || {}, header, config, dataType)
  }
}
export default APIClientService
